require('../common/header/index');
require('../common/sidebar/index');
var _product_service = require('../../service/product-service')
var _header_service = require('../../service/header-service')
var _cart_service = require('../../service/cart-service');
const vue = require("../common/vue")
let _main = new vue({
    el: '#myCarousel',
    data() {
        return {
            swiperList: [],
        }
    },
    methods: {
        getSwiperPic: function () {
            return this.swiperList;
        }
    }
})
let _mainbody = new vue({
    el: '#span9',
    data() {
        return {
            popularItems: [],
            newItems: [],
            pages: [0, 1, 2]
        }
    },
    methods: {
        getPopularItems: function (id) {
            let popular = [];
            for (let i = id * 4; i < id * 4 + 4; i++) {
                popular[i - id * 4] = this.popularItems[i];
            }
            return popular;
        },
        getNewItems: function () {
            return this.newItems
        },
        addToCart: function (itemid) {
            _product_service.getOneItem(itemid, function (res) {
                let attrnum = res.length;
                let attrdescription = "";
                let price = res[0].price * res[0].specialrate;
                for (let i = 0; i < attrnum - 1; i++) {
                    attrdescription = attrdescription + " " + res[i].attrList[0].attrvalue;
                    price = price * res[i].attrList[0].rate;
                }
                attrdescription = attrdescription + " 单位" + res[attrnum - 1].attrList[0].attrvalue;
                let content = {
                    num: 1,
                    price: price,
                    attrdescription: attrdescription
                }
                _cart_service.addToCart(itemid, function (res) {
                    window.location.href = "/view/product_summary.html";
                }, function (err) {
                    console.log(err);
                }, content);
            }, function (err) {
                console.log(err);
            });
        }
    }
})
var _catalog_main = {
    _this: this,
    init: function () {
        this.bindEvents();
        this.getSwiperPic();
        this.getPopularItems();
        this.getNewItems();
    },
    bindEvents: function () {

    },
    getSwiperPic: function () {
        _product_service.getSwiperPic(
            function (res) {
                _main.swiperList = res;
                for (let i = 0; i < _main.swiperList.length; i++) {
                    var pair = _main.swiperList[i].src.split("\"");
                    _main.swiperList[i].src = "../image/" + pair[1].substring(7);
                }
            },
            function (errMsg) {
                console.log(errMsg)
            }
        )
    },
    getPopularItems: function () {
        _product_service.getPopularItems(12,
            function (res) {
                _mainbody.popularItems = res
                for (let i = 0; i < _mainbody.popularItems.length; i++) {
                    var pair = _mainbody.popularItems[i].src.split("\"");
                    _mainbody.popularItems[i].src = "../image/" + pair[1].substring(7);
                }
            },
            function (errMsg) {
                console.log(errMsg)
            }
        )
    },
    getNewItems: function () {
        _header_service.searchItems(6, 1, '夏', 0, 0,
            function (res) {
                _mainbody.newItems = res.records;
                for (let i = 0; i < _mainbody.newItems.length; i++) {
                    var pair = _mainbody.newItems[i].src.split("\"");
                    _mainbody.newItems[i].src = "../image/" + pair[1].substring(7);
                }
            },
            function (errMsg) {
                console.log(errMsg)
            }
        )
    }
}
module.exports = _catalog_main.init();